<template>
	<Layout style="padding:0 24px 24px;height:100%;">
		<Breadcrumb style="margin:24px 0">
			<BreadcrumbItem>首页</BreadcrumbItem>
			<BreadcrumbItem>数据字典-父级</BreadcrumbItem>
		</Breadcrumb>
		<Form inline>
			<ButtonGroup style="margin-right:24px;">
				<Button title="新增" icon="md-add" @click="editModal = true"></Button>
				<Button title="删除" :disabled="selection.length == 0" icon="md-trash" @click="removeBatch"></Button>
			</ButtonGroup>
			<FormItem><Input type="text" placeholder="字典代码" clearable v-model="pageRequest.condition.dictCode" /></FormItem>
			<FormItem><Input type="text" placeholder="字典值" clearable v-model="pageRequest.condition.dictValue" /></FormItem>
			<FormItem><Button type="primary" icon="ios-search" shape="circle" title="查询" @click="list"></Button></FormItem>
		</Form>
		<Content ref="content" style="padding:0px;background:#fff;height:100%">
			<Table :loading="loading" :columns="tableColumns" :data="pageData.content" :height="tableHeight" @on-selection-change="onSelectionChange" border>
				<template slot-scope="{ row, index }" slot="action">
					<Button type="warning" size="small" style="margin-right: 5px" @click="modify(row)">修改</Button>
					<Button type="error" size="small" style="margin-right: 5px" @click="remove(row)">删除</Button>
					<Button type="success" size="small" @click="gotoPage(row)">子级</Button>
				</template>
				<template slot="footer">
					<Page style="margin-left:10px;" :total="pageData.total" :current="pageData.page" :page-size="15" @on-change="changePage"></Page>
				</template>
			</Table>
		</Content>
		<!-- 编辑对话框 -->
		<Modal :width="400" v-model="editModal" @on-visible-change="onVisibleChange" title="编辑父级字典项" footer-hide>
			<Form ref="dictForm" :model="dictInfo" :label-width="80" :rules="dictRules">
				<FormItem label="字典代码" prop="dictCode"><Input v-model="dictInfo.dictCode" type="text" placeholder="请输入字典代码" /></FormItem>
				<FormItem label="字典值" prop="dictValue"><Input v-model="dictInfo.dictValue" type="text" placeholder="请输入字典值" /></FormItem>
				<FormItem label="附加值" prop="addValue"><Input v-model="dictInfo.addValue" type="text" placeholder="请输入附加值" /></FormItem>
				<FormItem label="字典描述" prop="dictDesc"><Input v-model="dictInfo.dictDesc" type="text" placeholder="请输入字典描述" /></FormItem>
				<FormItem label="序号" prop="sequence"><Input v-model="dictInfo.sequence" type="number" placeholder="请输入序号" /></FormItem>
				<FormItem><Button type="primary" @click="save">保存</Button></FormItem>
			</Form>
		</Modal>
	</Layout>
</template>

<script language="JavaScript">
export default {
	components: {},
	data() {
		return {
			tableHeight: 100, // 自适应表格高度
			loading: false, // 加载中
			tableColumns: [
				{
					type: 'selection',
					width: 80
				},
				{
					title: '字典代码',
					key: 'dictCode',
					width: 250,
					resizable: true
				},
				{
					title: '字典值',
					key: 'dictValue',
					width: 250,
					resizable: true
				},
				{
					title: '附加值',
					key: 'addValue',
					width: 250,
					resizable: true
				},
				{
					title: '字典描述',
					key: 'dictDesc',
					minWidth: 250,
					width: 'auto',
					resizable: true
				},
				{
					title: '序号',
					key: 'sequence',
					width: 100,
					resizable: true
				},
				{
					title: '操作',
					slot: 'action',
					fixed: 'right',
					width: 250
				}
			], // 表格列
			// 分页请求
			pageRequest: {
				page: 1,
				rows: 15,
				sort: 'sequence:asc',
				condition: {}
			},
			// 分页查询结果
			pageData: {
				page: 1,
				total: 2,
				content: [] // 表格数据
			},
			editModal: false, // 编辑对话框显示
			dictInfo: {
				dictCode: '',
				dictValue: '',
				addValue: '',
				dictDesc: '',
				sequence: 0
			}, // 保存信息
			dictRules: {
				dictCode: [
					{
						required: true,
						message: '必填'
					}
				],
				dictValue: [
					{
						required: true,
						message: '必填'
					}
				],
				sequence: [
					{
						required: true,
						message: '必填'
					}
				]
			}, // 验证规则
			selection: [] //已选记录
		};
	},
	computed: {},
	watch: {
		'dictInfo.dictCode': function() {
			this.dictInfo.dictCode = this.dictInfo.dictCode.toUpperCase();
		}
	},
	created: function() {},
	mounted: function() {
		this.$nextTick(() => {
			this.tableHeight = this.$refs.content.$el.clientHeight;
			this.list();
		});
	},
	methods: {
		// 保存
		save: function() {
			this.$request.post(
				'/admin/sys/dict/save',
				this.dictInfo,
				res => {
					this.editModal = false;
					this.list();
				},
				true,
				true
			);
		},
		// 获取列表
		list: function() {
			this.$request.post(
				'/admin/sys/dict/list',
				this.pageRequest,
				res => {
					this.pageData = res.data || this.pageData;
				},
				false,
				false
			);
		},
		// 修改
		modify: function(row) {
			this.editModal = true;
			this.$nextTick(() => {
				this.dictInfo = Object.assign({}, row);
			});
		},
		// 删除
		remove: function(row) {
			this.$Modal.confirm({
				title: '确认？',
				content: '删除后不可恢复，是否继续？',
				onOk: () => {
					this.$request.post(
						'/admin/sys/dict/delete',
						row,
						res => {
							this.list();
						},
						true,
						true
					);
					this.selection = [];
				}
			});
		},
		// 批量删除
		removeBatch: function() {
			let ids = this.$utils.getPropertyArrayFromObjectArray(this.selection, 'id');
			if (!ids || ids.length == 0) {
				this.$Message.warning('请选择字典项');
				return;
			}
			this.$Modal.confirm({
				title: '确认？',
				content: '删除后不可恢复，是否继续？',
				onOk: () => {
					this.$request.post(
						'/admin/sys/dict/deleteBatch',
						ids,
						res => {
							this.list();
						},
						true,
						true
					);
					this.selection = [];
				}
			});
		},
		// 关闭对话框
		onVisibleChange: function() {
			this.$refs['dictForm'].resetFields();
			this.dictInfo.id = '';
		},
		// 表格多选事件
		onSelectionChange: function(sel) {
			this.selection = sel;
		},
		// 分页
		changePage: function(page) {
			this.pageRequest.page = page;
			this.list();
		},
		//跳转子级页面
		gotoPage: function(row) {
			this.$router.push({
				name: 'system_childDict',
				params: {
					parent: row
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped="scoped"></style>
